﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LamSon.Controllers
{
    public class KhachHangController : BaseController
    {
        //
        // GET: /KhachHang/
        BaseController bs = new BaseController();
        public ActionResult KhachHang()
        {
            return bs.BaseIndex(6, "KhachHang");
        }

        public static List<Data.DBOES.Membership> getListKhachHang(string khachhang)
        {
            Data.DBOES.Membership objCon = new Data.DBOES.Membership();
            objCon.IdRole = 5;
            if(string.IsNullOrEmpty(khachhang))
            {
            objCon.IdMembership =  null ;
            }
            else
            {
            objCon.IdMembership = Commons.DataTypeProtect.ProtectInt32(khachhang);
            }
            
            objCon.SortBy = "MembershipName";
            objCon.SortType = Core.SortType.ASC;
            List<Data.DBOES.Membership> objList = Data.Controllers.MembershipController.FindItems(objCon, BaseController.Pagecurr, BaseController.LimitPage, ref BaseController.totalRow);
            return objList;
        }
        public ActionResult Delete(string idKhachHang)
        {
            Data.DBOES.Membership objCon = Data.Controllers.MembershipController.FindItem(new Data.DBOES.Membership(Commons.DataTypeProtect.ProtectInt32(idKhachHang)));
            if (objCon != null)
                objCon.Delete();
            ReturnViewData_KhachHang(null);
            return PartialView("ListKhachHang");
        }
        public ActionResult Edit(string idKhachHang_Edit, string khachhang_sendedit, string pagesize_sendedit, string page_sendedit)
        {
            Data.DBOES.Membership objCon = Data.Controllers.MembershipController.FindItem(new Data.DBOES.Membership(Commons.DataTypeProtect.ProtectInt32(idKhachHang_Edit)));
            ViewData["DataKhachHang"] = objCon;
            SetViewDataInfoSearch(khachhang_sendedit,pagesize_sendedit, page_sendedit);
            return PartialView("FormKhachHang");

        }
        public ActionResult OK(string idKhachHang_OK)
        {
            Data.DBOES.Membership objCon = Data.Controllers.MembershipController.FindItem(new Data.DBOES.Membership(Commons.DataTypeProtect.ProtectInt32(idKhachHang_OK)));
            if (objCon != null)
            {
                objCon.IsActive = true;
                objCon.Update();
            }
            ReturnViewData_KhachHang(null);
            return PartialView("ListKhachHang");
        }
        //
        // GET: /NhatKyBanHang/Create
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Update(string IdKhachHang, string TenKhachHang, string DiaChi, string DienThoai, string Email,
            string khachhang_edit, string pagesize_edit, string page_edit
            )
        {
            Data.DBOES.Membership objCon;
            if (!string.IsNullOrEmpty(IdKhachHang))
            {
                objCon = Data.Controllers.MembershipController.FindItem(new Data.DBOES.Membership(Commons.DataTypeProtect.ProtectInt32(IdKhachHang)));
            }
            else
            {
                objCon = new Data.DBOES.Membership();
            }
            objCon.MembershipName = HttpUtility.HtmlEncode(Commons.Commons.RemoveHtmlTag(TenKhachHang));
            objCon.Address = HttpUtility.HtmlEncode(Commons.Commons.RemoveHtmlTag(DiaChi));
            objCon.Phone = HttpUtility.HtmlEncode(Commons.Commons.RemoveHtmlTag(DienThoai));
            objCon.Email = HttpUtility.HtmlEncode(Commons.Commons.RemoveHtmlTag(Email));
            objCon.IdRole = 5;
            objCon.IdLanguage = 1;
            objCon.CreateDate = DateTime.Now;
            if (!string.IsNullOrEmpty(IdKhachHang))
            {
                objCon.IsActive = false;
                objCon.Update();
            }
            else
            {
                objCon.IsActive = true;
                objCon.Insert();
            }
            BaseController.LimitPage = Commons.DataTypeProtect.ProtectInt32(string.IsNullOrEmpty(pagesize_edit)?"50":pagesize_edit);
            BaseController.Pagecurr = Commons.DataTypeProtect.ProtectInt32(string.IsNullOrEmpty(page_edit) ? "1" : page_edit);
            ReturnViewData_KhachHang( khachhang_edit);
            return PartialView("ListKhachHang");
        }
        public void SetViewDataInfoSearch(string khachhang_sendedit, string pagesize_sendedit, string page_sendedit)
        {
            ViewData["khachhang_sendedit"] = khachhang_sendedit;
            ViewData["pagesize_sendedit"] = pagesize_sendedit;
            ViewData["page_sendedit"] = page_sendedit;
        }
        public ActionResult LoadPage(string page ,string khachhang_paging)
        {
            BaseController.Pagecurr = LamSon.Commons.DataTypeProtect.ProtectInt32(page);
            ReturnViewData_KhachHang( khachhang_paging);
            return PartialView("ListKhachHang");
        }
        public ActionResult LoadLimitPage(string pagesize, string khachhang_limit)
        {
            BaseController.LimitPage = LamSon.Commons.DataTypeProtect.ProtectInt32(pagesize);
            BaseController.Pagecurr = 1;
            ReturnViewData_KhachHang(khachhang_limit);
            return PartialView("ListKhachHang");
        }
        public ActionResult LoadPageSearch( string khachhang)
        {
            BaseController.Pagecurr = 1;
            ReturnViewData_KhachHang(khachhang);
            return PartialView("ListKhachHang");
        }
        public ActionResult LoadPaging()
        {
            if (totalRow % LimitPage == 0)
            {
                ViewData["TotalPage"] = totalRow / LimitPage;
            }
            else
            {
                ViewData["TotalPage"] = totalRow / LimitPage + 1;
            }
            ViewData["PageCurr"] = BaseController.Pagecurr;

            return PartialView("Paging");
        }
        public void ReturnViewData_KhachHang(string khachhang_paging)
        {
            ViewData["IdForm"] = 6;
            ViewData["ListKhachHang"] = KhachHangController.getListKhachHang(khachhang_paging);
            ViewData["PageCurr"] = BaseController.Pagecurr;
            ViewData["LimitPage"] = BaseController.LimitPage;
            if (totalRow % LimitPage == 0)
            {
                ViewData["TotalPage"] = totalRow / LimitPage;
            }
            else
            {
                ViewData["TotalPage"] = totalRow / LimitPage + 1;
            }
            ViewData["DataKhachHang"] = null;
            ViewData["KhachHang"] = NhatKyBanHangController.getListCustomer(5);
        }
       
    }
}
